<?php


namespace App\Http\Services\Admin;


use App\Http\Constants\ApplicationStatusConstants;
use App\Http\Constants\ClosingRuleConstants;
use App\Http\Constants\VerificationModeConstants;
use App\Models\CoinsApplication;
use YiluTech\ComplexSearch\ComplexSearch;

class CoinsApplicationService extends ComplexSearch
{
    public $root = CoinsApplication::class;
    protected $display = 'simple';
    protected $orderBy = 'id desc';

    protected $headers = [
        [
            ['value' => 'id', 'label' => 'ID'],
            ['value' => 'name', 'label' => '应用名称'],
            ['value' => 'description', 'label' => '应用描述'],
            ['value' => 'app_id', 'label' => '应用appid'],
            ['value' => 'status', 'label' => '状态'],
            ['value' => 'verify_type', 'label' => '应用验证模式'],
            ['value' => 'verify_url', 'label' => '自定义验证路径'],
            ['value' => 'settle_rule_id', 'label' => '结算规则id'],
            ['value' => 'created_at', 'label' => '创建时间'],
            ['value' => 'update_at', 'label' => '修改时间'],
        ]
    ];

    protected $conditions = [
        'name' => [
            'label' => '应用名称',
            'ctype' => 'input',
            'operator' => 'like'
        ],
        'app_id' => [
            'label' => '应用APPId',
            'ctype' => 'input',
            'operator' => 'like'
        ],
        'status' => [
            'label' => '状态',
            'ctype' => 'select',
            'operator' => ApplicationStatusConstants::STATUS,
        ],
        'created_at' => [
            'label' => '创建时间',
            'ctype' => 'date-in',
            'display' => true,
            'format' => 'Y-m-d',
        ],
    ];

    public function query()
    {
        $select = ['id', 'name', 'app_id', 'description', 'created_at', 'status', 'verify_type'];
        $query = parent::query();
        $query->select($select);

        return $query;
    }

    /**
     * Description：应用详情
     * Date：2021-12-3 11:01
     * @param $id
     * @return mixed
     */
    public function detail($id)
    {
        $select = ['id', 'name', 'app_id', 'description', 'created_at', 'updated_at', 'status', 'settle_rule_id', 'verify_type', 'is_manual', 'is_deduct'];
        $result = CoinsApplication::select($select)->find($id);
        $result = $result->toArray();
        $result['get_settle_rule_business'] = [];
        $result['get_settle_rule_config'] = [];
        return $result;
    }

    /**
     * Description：创建应用
     * @param $data
     * @return mixed
     */
    public function create($data)
    {
        $data['app_id'] = md5(uniqid(microtime(true), true));
        return CoinsApplication::create($data);

    }

    /**
     * Description：更新应用
     * @param $data
     * @return mixed
     */
    public function update($data)
    {
        $CoinsApplication = CoinsApplication::find($data['id']);
        unset($data['app_id']);
#TODO：日志添加
        $CoinsApplication->fill($data);
        $CoinsApplication->save();
        return $CoinsApplication;

    }

    /**
     * Description：应用开启
     * @param $data
     * @return mixed
     */
    public function open($id)
    {
        $CoinsApplication = CoinsApplication::find($id);
        $CoinsApplication->status = ApplicationStatusConstants::START;
#TODO：日志添加
        $CoinsApplication->save();
        return $CoinsApplication;

    }

    /**
     * Description：应用关闭
     * @param $data
     * @return mixed
     */
    public function close($id)
    {
        $CoinsApplication = CoinsApplication::find($id);
        $CoinsApplication->status = ApplicationStatusConstants::STOP;
#TODO：日志添加
        $CoinsApplication->save();
        return $CoinsApplication;

    }

    /**
     * Description：是否支持手动增加红包
     * @param $data
     * @return mixed
     */
    public function is_manual($data)
    {
        $CoinsApplication = CoinsApplication::find($data['id']);
        $CoinsApplication->is_manual = (int)$data['is_manual'];
#TODO：日志添加
        $CoinsApplication->save();
        return $CoinsApplication;

    }

    /**
     * Description：是否支持红包抵扣
     * @param $data
     * @return mixed
     */
    public function is_deduct($data)
    {
        $CoinsApplication = CoinsApplication::find($data['id']);
        $CoinsApplication->is_deduct = (int)$data['is_deduct'];
#TODO：日志添加
        $CoinsApplication->save();
        return $CoinsApplication;

    }

    public function verify_type($data)
    {
        $CoinsApplication = CoinsApplication::find($data['id']);
        $CoinsApplication->verify_type = (int)$data['verify_type'];
#TODO：日志添加
        $CoinsApplication->save();
        return $CoinsApplication;
    }

    public function rule_update($data)
    {
        $CoinsApplication = CoinsApplication::find($data['id']);
        $CoinsApplication->settle_rule_id = (int)$data['settle_rule_id'];
#TODO：日志添加
        $CoinsApplication->save();
        return $CoinsApplication;
    }

    /**
     * Description：验证模式
     * @return array[]
     */
    public function getVerificationMode()
    {
        return VerificationModeConstants::VERIFICATION_MODE;
    }

    /**
     * Description：结算规则
     * @return array[]
     */
    public function getClosingRule()
    {
        return ClosingRuleConstants::CLOSING_RULE;
    }

}
